home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 15619 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.1 KB  |  88 lines

  1. Path: dispatch.news.demon.net!demon!dekard.demon.co.uk
  2. From: mat@dekard.demon.co.uk (Matt at Home)
  3. Newsgroups: comp.lang.c++
  4. Subject: Exceptions *NOT* being handled properly?!?
  5. Date: Sun, 07 Apr 1996 18:12:44 GMT
  6. Organization: Information Services, The University of North London.
  7. Message-ID: <31680560.1181640@news.demon.co.uk>
  8. NNTP-Posting-Host: dekard.demon.co.uk
  9. X-NNTP-Posting-Host: dekard.demon.co.uk
  10. X-Newsreader: Forte Agent .99e/32.194
  11.  
  12. Compiler: gnu g++ v2.7.2 (i386-univel-sysv4.2MP)
  13. Environment: UnixWare 2.03 Application Server (16Mb Ram,486DX2-80)
  14.  
  15. Problem: Exceptions do not seem to be handled correctly (according to
  16. FAQ 261 of Cline & Lomow)
  17.  
  18. Given the following code
  19.  
  20.     class X {};
  21.     class X2 : public X {};    // X2 is derived from X
  22.     ....
  23.     try
  24.     {
  25.         throw X2();    // throw the derived class
  26.     }
  27.     catch( const X2& e )    // catch the derived class
  28.     {
  29.         cerr << "Caught exception: Type X2" << endl;
  30.     }
  31.     catch( const X& e )    // catch the base class
  32.     {
  33.         cerr << "Caught exception: Type X" << endl;
  34.     }
  35.     catch( ... )        // catch anything left over
  36.     {
  37.         cerr << "Caught exception: Unknown type" << endl;
  38.     };
  39.     ....
  40.  
  41. Running this code correctly gives the output
  42.  
  43.     "Caught exception: Type X2"
  44.  
  45. However if we modify to remove the catch-block for X2 giving :-
  46.  
  47.     try
  48.     {
  49.         throw X2();    // throw the derived class
  50.     }
  51.     catch( const X& e )    // catch the base class
  52.     {
  53.         cerr << "Caught exception: Type X" << endl;
  54.     }
  55.     catch( ... )        // catch anything left over
  56.     {
  57.         cerr << "Caught exception: Unknown type" << endl;
  58.     };
  59.     ....
  60.  
  61. According to common sense AND to Cline & Lomow FAQ261, I expected :-
  62.  
  63.     "Caught exception: Type X"
  64.  
  65. Because X2 is a class derived from X ( i.e. we are catching an
  66. exception through the base class, Cline & Lomows xmsg)
  67.  
  68. But instead I get :-
  69.  
  70.     "Caught exception: Unknown type"
  71.  
  72. Can someone tell me :-
  73.  
  74. 1. What the correct behaviour is?
  75. 2. Is there something wrong with the code I have written (I have
  76. similar but *more functional* code which failes the same way in a
  77. project)?
  78. 3. Is there something wrong with the g++ compiler?
  79. 4. If there is, is it likely to be fixed?
  80.  
  81. Regards,
  82.  
  83. "Confused"
  84.  
  85. ----
  86. Matt Mower - Information Service Team
  87. M19d.  The University of North London.
  88.